/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vce.election.web;

import com.vce.election.Election;
import com.vce.election.domain.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
 *
 * @author Smahmud
 */
public class DocumentServlet extends HttpServlet {

    private Election election;
    private ApplicationContext appContext;

    static class Response {

        boolean success;
        String message;

        public Response() {
            success = true;
        }

        public Response(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
    }

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void activate(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (appContext == null) {
            appContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            election = (Election) appContext.getBean("election");
        }
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
        response.setHeader("Pragma", "no-cache"); //HTTP 1.0
        response.setDateHeader("Expires", 0); //prevents caching at the proxy server

        String jrvDescription = request.getParameter("autocomplete_jrv");
        Long ballotId = Long.valueOf(request.getParameter("ballot"));
        String counting = request.getParameter("counting");
        String operation = request.getParameter("operation");
        System.out.println("Operation is " + operation);

        if (counting == null) {
            counting = "RAPIDO";
        }

        if (operation == null) {
            operation = "INGRESO";
        }

        List<Jrv> jrvs = null;

        if (!isBlank(jrvDescription)) {
            if (ballotId != null) {
                BallotCentre ballot = election.getBallotCentre(ballotId);

                if (!ballot.getId().equals(0L)) {
                    if (operation.equals("INGRESO")) {
                        jrvs = election.findJrvs(jrvDescription, ballot, Counting.valueOf(counting), Round.PRIMERA);
                    } else {
                        jrvs = election.findRegisteredJrvs(jrvDescription, ballot, Counting.valueOf(counting), Round.PRIMERA);
                    }
                } else {
                    if (operation.equals("INGRESO")) {
                        jrvs = election.findJrvs(jrvDescription, election.getCurrentUser(), Counting.valueOf(counting), Round.PRIMERA);
                    } else {
                        jrvs = election.findInsertedJrvs(jrvDescription, election.getCurrentUser(), Counting.valueOf(counting), Round.PRIMERA);
                    }
                }
            }

            if (jrvs != null && jrvs.size() > 0) {
                response.getWriter().append("<ul>");
                for (Jrv jrv : jrvs) {
                    response.getWriter().append("<li id=\"" + jrv.getId() + "\">" + jrv.getCode() + "</li>");
                }
                response.getWriter().append("</ul>");
                response.getWriter().flush();
                response.getWriter().close();
            } else {
                response.getWriter().append("<ul>");
                if (jrvs.isEmpty()) {
                    response.getWriter().append("<li>No hay registros</li>");
                }
                response.getWriter().append("</ul>");
                response.getWriter().flush();
                response.getWriter().close();
            }
        }
    }

        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /** Handles the HTTP <code>GET</code> method.
         * @param request servlet request
         * @param response servlet response
         */
    protected

     void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}